********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************

/// MERGING ALL BASELINE DATA SETS ///


// Combine Teacher and Director survey and then match with games


use "$Baseline_dtRaw/_intermediate files/baseline_teachers.dta", clear

*** append

append using "$Baseline_dtRaw/_intermediate files/baseline_director.dta"

lab def survey_type 2 "Teacher" 3 "Director"
lab val survey_type survey_type
lab var survey_type "Survey type"

/*

order preschool grade1 grade2 grade3 grade4 grade5 grade6, after(director_type)

order Tbase_school_ocu Tbase_qualification, after(TDbase_transport_time)

order Tbase_teach_other_school_years Tbase_name_other_school 				///
	Tbase_loc1_other_school Tbase_loc2_other_school Tbase_transfer_reason 	///
	Tbase_teach_this_school_years Tbase_help_nomination 					///
	Tbase_help_nomination_type , after(TDbase_training_7)

order TDbase_edu TDbase_edu_uni_years TDbase_edu_pos_years, 				///
	after(TDbase_arriving_home)

order Tbase_consequence_good_1 Tbase_consequence_good_2 					///
Tbase_consequence_good_3 Tbase_consequence_good_4 Tbase_consequence_good_5 	///
Tbase_consequence_good_6 , after(Dbase_consequence_good)

order Tbase_consequence_bad_1 Tbase_consequence_bad_2 						///
Tbase_consequence_bad_3 Tbase_consequence_bad_4 Tbase_consequence_bad_5 	///
Tbase_dir_can_dismiss , after(Dbase_consequence_bad)

order Tbase_other_income_type1 Tbase_other_income_type2 					///
	Tbase_other_income_type3 Tbase_other_income_type4 						///
	Tbase_other_income_type5 Tbase_other_income_type6 						///
	Tbase_other_income_type7 Tbase_other_income_type8, 						///
	after(Dbase_other_income_type)

order TD3base_01_01 TD3base_01_02 TD3base_02_00 TD3base_02_01 				///
	TD3base_02_02 TD3base_02_03 TD3base_02_04 TD3base_02_05 TD3base_02_06 	///
	TD3base_03_00 TD3base_03_01 TD3base_03_02 TD3base_03_03 TD3base_03_04 	///
	TD3base_03_05 TD3base_03_06 TD3base_04_00 TD3base_04_01 TD3base_04_02 	///
	TD3base_04_03 TD3base_04_04 TD3base_04_05 TD3base_04_06 TD3base_05_00 	///
	TD3base_05_01 TD3base_05_02 TD3base_05_03 TD3base_05_04 TD3base_05_05 	///
	TD3base_05_06 TD3base_06_01 TD3base_06_02 TD3base_06_03 TD3base_06_04  	///
	TD3base_06_05 TD3base_06_06 TD3base_06_07 TD3base_06_08  				///
	TD3base_06_08_other TD3base_07 TD3base_08 TD3base_09_01 TD3base_09_02  	///
	TD3base_10_00 TD3base_10_01 TD3base_10_02 TD3base_10_03 TD3base_10_04  	///
	TD3base_10_05 TD3base_10_06 TD3base_11 TD3base_12_00 TD3base_12_01  	///
	TD3base_12_02 TD3base_12_03 TD3base_12_04 TD3base_12_05 TD3base_12_06  	///
	TD3base_13_00_01 TD3base_13_01_01 TD3base_13_01_02 TD3base_13_02_01  	///
	TD3base_13_02_02 TD3base_13_03_01 TD3base_13_03_02 TD3base_13_04_01  	///
	TD3base_13_04_02 TD3base_13_05_01 TD3base_13_06_01 TD3base_14_00  		///
	TD3base_14_01 TD3base_14_02 TD3base_14_03 TD3base_14_04 TD3base_14_05  	///
	TD3base_14_06 TD3base_15 TD3base_15_00 TD3base_15_01 TD3base_15_02  	///
	TD3base_15_03 TD3base_15_04 TD3base_15_05 TD3base_15_06 TD3base_16_00  	///
	TD3base_16_01 TD3base_16_02 TD3base_16_03 TD3base_16_04 TD3base_16_05  	///
	TD3base_16_06 TD3base_17 TD3base_18_01 TD3base_18_02 TD3base_18_03  	///
	TD3base_19_00 TD3base_19_01 TD3base_19_02 TD3base_19_03 TD3base_19_04  	///
	TD3base_19_05 TD3base_19_06 TD3base_20 TD3base_21_00 TD3base_21_01  	///
	TD3base_21_02 TD3base_21_03 TD3base_21_04 TD3base_21_05 TD3base_21_06  	///
	TD3base_22 TD3base_23 TD3base_24 TD3base_25 TD3base_26 TD3base_27  		///
	TD3base_28 TD3base_29 TD3base_30 TD3base_31_01 TD3base_31_01_other  	///
	TD3base_31_02 TD3base_31_02_other TD3base_31_03 TD3base_31_03_other  	///
	TD3base_32 TD3base_33 TD3base_34 TD3base_35 TD3base_36 TD3base_37  		///
	TD3base_38 TD3base_39 TD3base_40 TD3base_41 TD3base_42 TD3base_43,  	///
	after(TDbase_other_income_value)

order TDbase_04_08_01 TDbase_04_08_02, after(TDbase_04_08)

order TDbase_04_12_02_gift_specified, after(TDbase_04_12_02_gift)

order TDbase_school_good_man TDbase_school_bad_man_reason1  				///
TDbase_school_bad_man_reason2, after(TDbase_teacher_studentresults)

order TD12base_07_02_comuna TD12base_07_02_other, after(TD12base_07_02)

order TD5base_04 TD5base_05_01 TD5base_05_02 TD5base_05_03 TD5base_05_04  	///
TD5base_05_04_other TD5base_06, after(TD5base_03_11)

order TD5base_10_a TD5base_10_b TD5base_10_01 TD5base_10_02 TD5base_10_03  	///
TD5base_10_04 TD5base_10_05 TD5base_10_06 TD5base_10_07 TD5base_10_08  		///
TD5base_10_09 TD5base_10_10 TD5base_10_11 TD5base_11_01 TD5base_11_02  		///
TD5base_11_03 TD5base_11_04 TD5base_11_05 TD5base_11_06 TD5base_11_07  		///
TD5base_11_08 TD5base_11_09 TD5base_11_10 TD5base_11_11 TD5base_12_01  		///
TD5base_12_02 TD5base_12_03 TD5base_12_04 TD5base_12_05 TD5base_12_06  		///
TD5base_12_07 TD5base_12_08 TD5base_12_09 TD5base_12_10 TD5base_12_11,  	///
after(TD5base_09_11)

*/

replace resp_idgame = "P026_5" if resp_name=="agela cipriano manuel vieira"

replace resp_idgame = "P067_4" if resp_name=="domingos jose papagaio"
replace resp_idgame = "P067_2" if resp_name=="julia maria mendez cabriel"
replace resp_idgame = "P067_1" if resp_name=="quintino joaquim kitutu"
replace resp_idgame = "P067_5" if resp_name=="enis jose cafranca"
replace resp_idgame = "" if resp_name=="Domingos Prazeres Antonio"
replace resp_idgame = "P063_1" if resp_name=="DOMINGOS PRAZERIS ANTONIO"


** add games data

merge m:1 resp_idgame using "$Baseline_dtRaw/_intermediate files/baseline_teachersgame.dta"

drop _merge newid school_name resp_idprof resp_name_survey survey matched resp_id

sort school_id

foreach var in 	date A1 A2_received sent200 sent400 sent600 sent800 		///
				faces A3 expect_group true_group{
		rename `var' Gtdbase_`var'
}

foreach var in 	survey_type resp_idgame time_start 	///
				time_finish resp_name director_type preschool grade1 grade2 ///
				grade3 grade4 grade5 grade6{
		rename `var' TDbase_`var'
}

save "$Baseline_dtRaw/_intermediate files/baseline_TDG.dta", replace




// Generating baseline outcome variables

// School level

use "$Baseline_dtRaw/_intermediate files/admin16.dta", clear

keep school_id A16_teachers A16_classrooms A16_students A16_highest_grade

rename A16_* A_*

save "$Baseline_dtRaw/_intermediate files/baselinevars_school.dta", replace


use "$Baseline_dtRaw/_intermediate files/baseline_school.dta", clear

keep 	school_id ///
		SQ8_02_01 SQ8_02_02 SQ8_02_03 SQ8_02_04 SQ8_02_05 SQ8_02_06 SQ8_02_07 SQ8_02_08 SQ8_02_09 SQ8_02_10 ///
		SQ2_06_a SQ2_07_a SQ2_14 SQ2_14_num SQ2_15_01 SQ2_15_02 SQ2_15_03 SQ2_09_dummy SQ3_desks SQ3_chairs SQ3_books_exc ///
		SQ6_04_01 SQ6_04_02 SQ6_04_03 SQ6_04_04 SQ6_04_05 SQ6_04_06 SQ6_04_07 SQ6_04_08 SQ4_students

rename SQ8_02_01 basebal_SQ8_02_01
rename SQ8_02_02 basebal_SQ8_02_02
rename SQ8_02_03 basebal_SQ8_02_03
rename SQ8_02_04 basebal_SQ8_02_04
rename SQ8_02_05 basebal_SQ8_02_05
rename SQ8_02_06 basebal_SQ8_02_06
rename SQ8_02_07 basebal_SQ8_02_07
rename SQ8_02_08 basebal_SQ8_02_08
rename SQ8_02_09 basebal_SQ8_02_09
rename SQ8_02_10 basebal_SQ8_02_10
rename SQ2_06_a basebal_SQ2_06_a
rename SQ2_07_a basebal_SQ2_07_a
rename SQ2_14 basebal_SQ2_14
rename SQ2_14_num basebal_SQ2_14_num
rename SQ2_15_01 basebal_SQ2_15_01
rename SQ2_15_02 basebal_SQ2_15_02
rename SQ2_15_03 basebal_SQ2_15_03
rename SQ2_09_dummy basebal_SQ2_09_dummy
rename SQ3_desks basebal_SQ3_desks
rename SQ3_chairs basebal_SQ3_chairs
rename SQ3_books_exc basebal_SQ3_books_exc

rename SQ6_04_01 S_info1
rename SQ6_04_02 S_info2
rename SQ6_04_03 S_info3
rename SQ6_04_04 S_info4
rename SQ6_04_05 S_info5
rename SQ6_04_06 S_info6
rename SQ6_04_07 S_info7
rename SQ6_04_08 S_info8

merge 1:1 school_id using "$Baseline_dtRaw/_intermediate files/baselinevars_school.dta"

drop _merge

save "$Baseline_dtRaw/_intermediate files/baselinevars_school.dta", replace

// Teachers

use "$Baseline_dtRaw/_intermediate files/baseline_TDG.dta", clear

// keep only teachers

drop if Dbase_teach==0

rename TD7base_14 		basebal_TD7base_14
rename TD3base_10_00 	basebal_TD3base_10_00
rename TD3base_10_01  basebal_TD3base_10_01
rename TD3base_10_02  basebal_TD3base_10_02
rename TD3base_10_03  basebal_TD3base_10_03
rename TD3base_10_04  basebal_TD3base_10_04
rename TD3base_10_05  basebal_TD3base_10_05
rename TD3base_10_06  basebal_TD3base_10_06
rename TD3base_17     basebal_TD3base_17
rename TD7base_04     basebal_TD7base_04
rename TD7base_05     basebal_TD7base_05
rename TD7base_06     basebal_TD7base_06
rename TD7base_25     basebal_TD7base_25

gen t = 0

rename TDbase_female T_female
rename TDbase_age T_age

gen T_highedu = 0 if TDbase_edu!=.
replace T_highedu = 1 if inlist(TDbase_edu,7,8,9,10,11) & TDbase_survey_type==2
replace T_highedu = 1 if inlist(TDbase_edu,5,6,7,8,9,10,11,12,13) & TDbase_survey_type==3

rename TDbase_children T_children

gen T_pt = 0 if TDbase_language!=.
replace T_pt = 1 if TDbase_language==1


rename TD12base_01 T_social

egen T_assets = rowtotal(TD14base_03_01 TD14base_03_02 TD14base_03_03 TD14base_03_04 TD14base_03_05 TD14base_03_06 TD14base_03_07 TD14base_03_08 TD14base_03_09 TD14base_03_10 TD14base_03_11 TD14base_03_12 TD14base_03_13 TD14base_03_14 TD14base_03_15 TD14base_03_16 TD14base_03_17), missing


keep 	school_id t ///
			T_female T_age T_highedu T_children T_pt T_social T_assets ///
			basebal_*

********************************************************************************

save "$Baseline_dtRaw/_intermediate files/baselinevars_TG.dta", replace

********************************************************************************

// Director

use "$Baseline_dtRaw/_intermediate files/baseline_TDG.dta", clear

keep if TDbase_survey_type==3

rename TD7base_04 basebal_TD7base_04_dir
rename TD7base_05 basebal_TD7base_05_dir
rename TD7base_06 basebal_TD7base_06_dir
rename TD7base_25 basebal_TD7base_25_dir

gen t = 0

rename TDbase_female D_female
rename TDbase_age D_age

gen D_highedu = 0 if TDbase_edu!=.
replace D_highedu = 1 if inlist(TDbase_edu,7,8,9,10,11) & TDbase_survey_type==2
replace D_highedu = 1 if inlist(TDbase_edu,5,6,7,8,9,10,11,12,13) & TDbase_survey_type==3

rename TDbase_children D_children

gen D_pt = 0 if TDbase_language!=.
replace D_pt = 1 if TDbase_language==1

rename TD12base_01 D_social

egen D_assets = rowtotal(TD14base_03_01 TD14base_03_02 TD14base_03_03 TD14base_03_04 TD14base_03_05 TD14base_03_06 TD14base_03_07 TD14base_03_08 TD14base_03_09 TD14base_03_10 TD14base_03_11 TD14base_03_12 TD14base_03_13 TD14base_03_14 TD14base_03_15 TD14base_03_16 TD14base_03_17), missing

keep 	school_id t ///
			D_female D_age D_highedu D_children D_pt D_social D_assets ///
			basebal_*

save "$Baseline_dtRaw/_intermediate files/baselinevars_DG.dta", replace

********************************************************************************

********************************************************************************

// Parents

use "$Baseline_dtRaw/_intermediate files/baseline_parents.dta", clear

drop if PQ2_01>19
drop if inlist(P_resp_relation,0,8,10)

gen t = 0

rename PQ6_12 basebal_PQ6_12
rename PQ6_13 basebal_PQ6_13
rename PQ3_12 basebal_PQ3_12
rename PQ2_12 basebal_PQ2_12
rename PQ9_09 basebal_PQ9_09
rename PQ4_16 basebal_PQ4_16
rename PQ7_07 basebal_PQ7_07
rename PQ4_04 basebal_PQ4_04
rename PQ4_05 basebal_PQ4_05
rename PQ4_06 basebal_PQ4_06
rename PQ4_25 basebal_PQ4_25

rename PQ1_02 P_female
rename PQ1_01 P_age

gen P_primedu = 1 if PQ1_08>3 & PQ1_08!=.
replace P_primedu = 0 if P_primedu==. & PQ1_08!=.

rename PQ1_14 P_children

gen P_pt = 0 if PQ1_19!=.
replace P_pt = 1 if PQ1_19==1

rename PQ10_01 P_social

egen P_assets = rowtotal(PQ12_03_01 PQ12_03_02 PQ12_03_03 PQ12_03_04 PQ12_03_05 PQ12_03_06 PQ12_03_07 PQ12_03_08 PQ12_03_09 PQ12_03_10 PQ12_03_11 PQ12_03_12 PQ12_03_13 PQ12_03_14 PQ12_03_15 PQ12_03_16 PQ12_03_17), missing

rename PQ2_01 P_student_age
rename PQ2_02 P_student_female
rename PQ1_23 P_timetoschool

keep 	school_id t ///
			P_female P_age P_primedu P_children P_pt P_social P_assets P_timetoschool ///
			P_student_female P_student_age ///
			basebal_*

********************************************************************************

append using "$Baseline_dtRaw/_intermediate files/baselinevars_TG.dta"

merge m:1 school_id using "$Baseline_dtRaw/_intermediate files/baselinevars_school.dta"

drop _merge

merge m:1 school_id using "$Baseline_dtRaw/_intermediate files/baselinevars_DG.dta"

drop _merge

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


gen municipality = .

replace municipality = 1 if inlist(school_id,1,2,3,4,5,6,7,8,9,10,12,13,	///
	14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,37,		///
	40,45,61,86,110)

replace municipality = 2 if inlist(school_id,23,43,44,46,47,48,49,50,51,	///
	52,53,54,55,56,57,58,59,60,62,63,64,65,66,67,68,69,70,71,72,73,75,76,	///
	77,78,79,80,81)

replace municipality = 3 if inlist(school_id,11,36,38,39,41,42,74,82,83,84,	///
	85,87,88,89,90,91,92,93,94,95,96,97,98,99,101,102,103,104,105)

replace municipality = 4 if inlist(school_id,118,119,120,121,122,123,124,125)
replace municipality = 5 if inlist(school_id,100,112,113,114,115,116,117)
replace municipality = 6 if inlist(school_id,109,111)
replace municipality = 7 if inlist(school_id,106,107)
replace municipality = 8 if inlist(school_id,126)
replace municipality = 9 if inlist(school_id,108)

gen treatment = .

replace treatment = 0 if inlist(school_id,3,4,7,10,16,18,20,23,26,29,33, ///
	39,42,53,55,62,63,64,65,78,81,84,87,88,89,92,98,108,109,116,117,121)

replace treatment = 1 if inlist(school_id,15,19,22,27,32,36,37,40,49,51, ///
	56,57,58,61,66,68,72,76,79,82,83,86,91,95,100,102,103,106,110,114,125,126)

replace treatment = 2 if inlist(school_id,1,5,6,11,12,14,21,24,25,31,41, ///
	48,50,52,59,70,73,77,80,90,93,94,101,105,107,111,112,113,119,120,124)

replace treatment = 3 if inlist(school_id,2,8,9,13,17,28,30,34,35,38,43, ///
	44,45,46,47,54,60,67,69,71,74,75,85,96,97,99,104,115,118,122,123)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

save "$Baseline_dtDeID/data_deidentified.dta", replace
